package com.zt.weibo;

import android.app.Activity;
import android.app.Application;
import android.os.Handler;
import bugreport.AppManager;
import bugreport.handler.CrashHandler;

import com.weibo.net.Weibo;
import com.zt.weibo.database.WeiboDatabaseMgr;
import com.zt.weibo.service.WeiboService;
import com.zt.weibo.utils.PrintLog;
import com.zt.weibo.utils.Utilities;

public class WeiboApp extends Application
{
	/**
	 * @see WeiboDatabaseMgr
	 */
	public static WeiboDatabaseMgr mDBMgr = null;
	/**
	 * @see WeiboService
	 */
	public static WeiboService mServer = null;
	/**
	 * @see Weibo
	 */
	public static Weibo mWeibo = null;
	/**
	 * @see Handler
	 */
	public static Handler mHandler = null;
	/**
	 * 自上次退出后是否为第一次启动
	 */
	public static boolean bIsFirstCreate = true;
	/**
	 * 用户是否存在
	 */
	public static boolean bIsUserExist = false;
	/**
	 * 用户授权是否过期
	 */
	public static boolean bIsUserInvalid = true;

	@Override
	public void onCreate()
	{
		super.onCreate();
		PrintLog.debug("WeiboApp On Create");
		mHandler = new Handler();
		mWeibo = Weibo.getInstance();
		mDBMgr = WeiboDatabaseMgr.getInstance(getApplicationContext());
		mServer = WeiboService.getInstance(mWeibo, getApplicationContext());
		Utilities.createShortCut(this);
		CrashHandler.getInstance().init(getApplicationContext());
	}

	@Override
	public void onTerminate()
	{
		super.onTerminate();
	}

	/**
	 * 加入指定{@link Activity}至列表中
	 * @param activity
	 */
	public static void addActivity(Activity activity)
	{
		AppManager.getInstance().insert(activity);
	}
	
	/**
	 * 从列表中删除指定{@link Activity}
	 * @param activity
	 */
	public static void delActivity(Activity activity)
	{
		AppManager.getInstance().remove(activity);
	}
	
	public static void exitApp()
	{
		PrintLog.debug("Exit App");
		mDBMgr.closeDB();
		mDBMgr = null;
		mServer = null;
		mWeibo = null;
		mHandler = null;
		bIsFirstCreate = true;
		AppManager.getInstance().exitApp();
	}

}
